package com.midian.action.mc.activity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Transient;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.hp.hpl.sparta.xpath.TrueExpr;
import com.midian.action.mc.common.AjaxAction;
import com.midian.common.annotation.FunctionAnnotationOverride;
import com.midian.common.exception.BizException;
import com.midian.model.activity.ActivityComment;
import com.midian.model.user.User;
import com.midian.service.activity.IActivityCommentService;

/**
 * 图书评论Action
 * @author Mengfh
 *
 */
@Scope("prototype")
@Controller("activityCommentAction")
@FunctionAnnotationOverride(isAction = true, auth = "评论管理")
public class ActivityCommentAction extends AjaxAction<ActivityComment> {
	/**图书评论实体*/
	private ActivityComment entity;
	/**图书评论实体列表*/
	private List<ActivityComment> rows;
	/**图书评论业务*/
	private IActivityCommentService activityCommentService;
	
	private List<String> picList = new ArrayList<String>();
	@Transient
	private String id;
	@Transient
	private String userid;
	@Transient
	private String activityid;
	
	
	@Transient
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public List<String> getPicList() {
		return picList;
	}
	public void setPicList(List<String> picList) {
		this.picList = picList;
	}
	public ActivityComment getEntity() {
		return entity;
	}
	public void setEntity(ActivityComment entity) {
		this.entity = entity;
	}
	@Resource(name = "activityCommentService")
	public void setActivityCommentService(IActivityCommentService activityCommentService) {
		this.service = activityCommentService;
		this.activityCommentService = activityCommentService;
	}
	@Override
	protected ActivityComment dytEntity() {
		if(this.entity == null) {
			this.entity = new ActivityComment();
		}
		return getEntity();
	}
	@Override
	protected void dyEntity(ActivityComment t) {
		setEntity(t);
	}
	@Override
	public void setRows(List<ActivityComment> rows) {
		this.rows = rows;
	}
	@Override
	public List<ActivityComment> getRows() {
		return rows;
	}
	
	@Override
	public String showInfo() {
		String activityCommentId=id;
		String userId = getUserIdById(id);
		String activityId = getActivityIdById(id);
		String[] values ={userId,activityId,activityCommentId};
		StringBuilder sql = new StringBuilder();
		sql.append("select u.account,p.id from activity_comment_pic acp")
			.append(" LEFT JOIN activity_comment ac on acp.commentId=ac.id")
			.append(" LEFT JOIN user u on ac.userId = u.id")
			.append(" LEFT JOIN pic p on acp.picId = p.id")
			.append(" LEFT JOIN activity a on ac.activityId=a.id")
			.append(" where u.id=? and a.id=? and ac.id=?");
		
		try {
			List list = this.activityCommentService.getResultsBysql(sql.toString(), values,null);
			for (int i = 0; i < list.size(); i++) {
				Map<String, Object> resultMap = (Map<String, Object>) list.get(i);
				String picid = (String) resultMap.get("id");
//				System.out.println("--------wert------"+picid);
				picList.add(picid);
			}
			
		} catch (BizException e) {
			e.printStackTrace();
		}
		return super.showInfo();
	}
	
//	@Override
//	public String delInfo() {
//		String userId = getUserIdById(id);
//		String activityid = getActivityIdById(id);
//		String[] values ={userId,activityid};
//		StringBuilder sql = new StringBuilder("delete from activity_comment ac "
//				+" INNER JOIN activity_comment_pic acp on ac.id=acp.commentId"
//				+" INNER JOIN pic p on acp.picId = p.id"
//				+" INNER JOIN user u on u.id = ac.userId"
//				+" INNER JOIN activity a on  ac.activityId=a.id"
//				+" where u.id=? and a.id=?");
//		try {
//			this.activityCommentService.deleteBySql(sql.toString(),values);
////			this.activityCommentService.getResultsBysql(sql.toString());
//		} catch (BizException e) {
//			e.printStackTrace();
//		}
//		return super.delInfo();
//	}
	
	/**双击后根据从页面传过来的评论内容id来获取到用户id*/
	public String getUserIdById(String id) {

		String[] values ={id};
		StringBuilder getUserIdSql=new StringBuilder("select userId from activity_comment where id = ?");
		try {
			List<Map<String, Object>> list = this.activityCommentService.getResultsBysql(getUserIdSql.toString(),values,null);
			 userid = (String) list.get(0).get("userId");
		} catch (BizException e) {
			e.printStackTrace();
		}
		return userid;
		
	}
	
	/**双击后根据从页面传过来的评论内容id来获取到活动id*/
	public String getActivityIdById(String id) {

		String[] values ={id};
		StringBuilder getActivityIdSql=new StringBuilder("select activityId from activity_comment where id = ?");
		try {
			List<Map<String, Object>> list = this.activityCommentService.getResultsBysql(getActivityIdSql.toString(),values,null);
			 activityid = (String) list.get(0).get("activityId");
		} catch (BizException e) {
			e.printStackTrace();
		}
		return activityid;
		
	}
	
	
	
}
